package com.yazi.mxz.common.service.impl;

import javax.annotation.Resource;

import com.myframework.smhj.common.exception.MyEntityNotExistsException;
import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.exception.MyNoPermissionException;
import com.myframework.smhj.common.tool.CommonUtil;
import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.dao.BasicService;
import com.myframework.smhj.hibernate.dao.Dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yazi.mxz.common.db.dao.MemberDao;
import com.yazi.mxz.common.db.dao.MemberRoleDao;
import com.yazi.mxz.common.db.dao.StoreDao;
import com.yazi.mxz.common.db.dao.StoreMemberDao;
import com.yazi.mxz.common.entity.ValueFactory;
import com.yazi.mxz.common.entity.beans.Company;
import com.yazi.mxz.common.entity.beans.MemberRole;
import com.yazi.mxz.common.entity.beans.MemberRoleId;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.entity.beans.StoreMember;
import com.yazi.mxz.common.service.StoreMemberService;

@Service("storeMemberService")
public class StoreMemberServiceImpl extends BasicService<StoreMember> implements StoreMemberService {
	private static Logger log = LoggerFactory.getLogger(StoreMemberServiceImpl.class);
	
	@Resource
	StoreMemberDao storeMemberDao;
	@Resource
	StoreDao storeDao;
	@Resource
	MemberDao memberDao;
	@Resource
	MemberRoleDao memberRoleDao;
	
	@Override
	protected Dao<StoreMember> getDao() {
		return storeMemberDao;
	}
	
	@Override
	public StoreMember addByCompany(Company company, StoreMember storeMember, 
			Long roleId) throws MyException {
		log.debug("addByCompany：companyId={},storeId={},memberId={},roleId={}", 
				new Object[]{company.getCompanyId(), storeMember.getId().getStoreId(), storeMember.getId().getMemberId(), roleId});
		
		Store store = storeDao.get(storeMember.getId().getStoreId());
		if (null == store) {
			throw new MyEntityNotExistsException();
		}
		if (!store.getCompany().equals(company)) {
			throw new MyNoPermissionException();
		}
		
		return add(storeMember, roleId);
	}
	
	@Override
	public StoreMember add(StoreMember storeMember, Long roleId) throws MyException {
		log.debug("add：storeId={},memberId={},roleId={}",
				new Object[]{storeMember.getId().getStoreId(), storeMember.getId().getMemberId(), roleId});
		
		// 店铺管理员数量检查
		Store store = storeDao.get(storeMember.getId().getStoreId());
		if (store.getMemRemain() <= 0) {
			throw new MyNoPermissionException("数量受限");
		}
		store.setMemRemain(store.getMemRemain() - 1);
		storeDao.merge(store);
		
		ValueFactory.validate(storeMember);
		storeMember.setPwd(CommonUtil.randomLN(6));
		storeMember.setAddTime(DateUtil.getTimeStamp());
		storeMember.setUpdateTime(DateUtil.getTimeStamp());
		storeMemberDao.persist(storeMember);
		
		// 将用户授予店铺管理角色
		if (null != roleId) {
			MemberRoleId mrId = new MemberRoleId();
			mrId.setMemberId(storeMember.getId().getMemberId());
			mrId.setRoleId(roleId);
			MemberRole mr = new MemberRole();
			mr.setId(mrId);
			memberRoleDao.persist(mr);
		}
		
		return storeMember;
	}

	@Override
	public void remove(StoreMember storeMember) {
		log.debug("remove：storeId={},memberId={}", 
				storeMember.getId().getMemberId(),storeMember.getId().getStoreId());
		
		// 删除目标对象
		storeMemberDao.delete(storeMember);
		
		// 店铺成员数
		Store store = storeMember.getStore();
		store.setMemRemain(store.getMemRemain() + 1);
		storeDao.merge(store);
		
		// 移除用户与店铺关联的角色
		memberRoleDao.deleteStoreRolesForMember(storeMember.getId().getMemberId(),
				storeMember.getId().getStoreId());
	}

	@Override
	public Page<StoreMember> listByGtUpdateTime(StoreMember sm, Searching si) {
		log.debug("listByGtUpdateTime：{}", sm.getUpdateTime());
		return storeMemberDao.listByGtUpdateTime(sm, si);
	}

}
